package jsc.structureDeDonnees;

import java.io.File;
import java.io.IOException;
import java.util.TreeSet;

import jsc.base.TextFileReader;

/**
 * This class is basicaly a TreeSet implementing StructureDico 
 * 
 * @author Alec Ermis, Maxime Rihouey
 *
 */
public class TreeSetString implements StructureDico{

	private TreeSet<String> tree;

	// constructors
	public TreeSetString(){
		tree = new TreeSet<String>();
	}

	public TreeSetString(String[] t){
		tree = new TreeSet<String>();
		this.ajouter(t);
	}

	// display
	public String toString(){
		return tree.toString();
	}

	// methods
	public void ajouter(String[] t){
		int taille = t.length;
		tree.add(t[taille/2]);
		this.ajouter(t, 0, taille/2-1);
		this.ajouter(t, taille/2+1, t.length-1);
	}

	public void ajouter(String[] t, int from, int to){
		if(from <= to){
			int taille = to-from;
			tree.add(t[from + taille/2]);
			this.ajouter(t, from, from + (taille/2) -1);
			this.ajouter(t, from + (taille/2)+1, to);
		}
	}

	@Override
	public void ajouter(String o) {
		tree.add(o);
	}

	@Override
	public boolean existe(String o) {
		return tree.contains(o);
	}

	@Override
	public String[] tableau() {
		// TODO Auto-generated method stub
		return (String[]) tree.toArray();
	}

	public static void main(String[] args) {

		System.out.println("###############################");

		try {


			TreeSetString arbre = new TreeSetString(new TextFileReader(new File("dicts/log.txt")).readerToArray());
			System.out.println(arbre);

			/*String[] tableau = arbre2.tableau();

			System.out.println(arbre2.tableau().length);
			for(int i = 0; i < tableau.length; i++){
				if(tableau[i] == null){
					System.out.println("NULLLLLLLLL"+" "+i);
				}else{
					System.out.println(tableau[i]+" "+i);
				}
			}

			arbre2.ajouter("Arrrrrrrrr");
			tableau = arbre2.tableau();
			System.out.println(arbre2.tableau().length);
			for(int i = 0; i < tableau.length; i++){
				if(tableau[i] == null){
					System.out.println("NULLLLLLLLL"+" "+i);
				}else{
					System.out.println(tableau[i]+" "+i);
				}
			}
			System.out.println("###############################");
			System.out.println(arbre2);
			System.out.println("###############################");

			arbre2.ajouter("laaaaaaaaaaal");
			tableau = arbre2.tableau();
			System.out.println(arbre2.tableau().length);
			for(int i = 0; i < tableau.length; i++){
				if(tableau[i] == null){
					System.out.println("NULLLLLLLLL"+" "+i);
				}else{
					System.out.println(tableau[i]+" "+i);
				}
			}
			System.out.println("###############################");
			System.out.println(arbre2);
			System.out.println("###############################");

			 */	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
